﻿using System;
using System.Web.Mvc;
using WD.Db.Contract;
using WD.Framework.Utility;
using WD.Framework.Web;
using WD.Web.MVC.Common;

namespace WD.Web.MVC.Areas.BaseArea.Controllers
{
    public class AuthController : AdminControllerBase
    {
        [AuthorizeIgnore]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        [AuthorizeIgnore]
        public ActionResult Login(string username, string password, string verifycode)
        {
            ViewBag.userId = username;
            ViewBag.pwd = password;
            if (!VerifyCodeHelper.CheckVerifyCode(verifycode, this.CookieContext.VerifyCodeGuid))
            {
                ModelState.AddModelError("error", "验证码错误");
                return View();
            }

            
            var loginInfo = this.Rbac_LoginInfoService.Login(username, password);

            if (loginInfo != null)
            {
                this.CookieContext.UserToken = loginInfo.LoginToken;
                this.CookieContext.UserName = loginInfo.LoginId;
                this.CookieContext.UserId = loginInfo.UserID;
                CookieContext.Type = loginInfo.EnumLoginAccountType;
                Logger(CommonCode.日志类型_登录, JsonP(loginInfo));
                return RedirectToAction("Index", "Home");
            }
            else
            {
                ModelState.AddModelError("error", "用户名或密码错误");
                return View();
            }
        }

        public ActionResult Logout()
        {
            this.Rbac_LoginInfoService.Logout(this.CookieContext.UserToken);
            this.CookieContext.UserToken = Guid.Empty;
            this.CookieContext.UserName = string.Empty;
            this.CookieContext.UserId = 0;
            return RedirectToAction("Login");
        }

        [HttpPost]
        [AuthorizeIgnore]
        public string LoginAjax(string username, string password, string verifycode)
        {
            var ajax = new AjaxResult();
            if (!VerifyCodeHelper.CheckVerifyCode(verifycode, this.CookieContext.VerifyCodeGuid))
            {
                return ajax.Err("验证码错误");
            }

            var loginInfo = this.Rbac_LoginInfoService.Login(username, password);

            if (loginInfo != null)
            {
                this.CookieContext.UserToken = loginInfo.LoginToken;
                this.CookieContext.UserName = loginInfo.LoginId;
                this.CookieContext.UserId = loginInfo.UserID;
                Logger(CommonCode.日志类型_登录, JsonP(loginInfo));
                return ajax.Suc("登录成功");
            }
            else
            {
                ModelState.AddModelError("error", "用户名或密码错误");
                return ajax.Err("用户名或密码错误");
            }
        }

        public string LogoutAjax()
        {
            var ajax = new AjaxResult();
            this.Rbac_LoginInfoService.Logout(this.CookieContext.UserToken);
            this.CookieContext.UserToken = Guid.Empty;
            this.CookieContext.UserName = string.Empty;
            this.CookieContext.UserId = 0;
            return ajax.Suc("已成功退出");
        }


        //        [AuthorizeIgnore]
        //        public ActionResult VerifyImage()
        //        {
        //            var s1 = new ValidateCode_Style4();
        //            string code = "6666";
        //            byte[] bytes = s1.CreateImage(out code);
        //
        //            this.CookieContext.VerifyCode = code;
        //
        //            return File(bytes, @"image/jpeg");
        //
        //        }

        [AuthorizeIgnore]
        public virtual ActionResult VerifyImage()
        {
            var validateCodeType = new ValidateCode_Style10();
            string code;
            var bytes = validateCodeType.CreateImage(out code);
            this.CookieContext.VerifyCodeGuid = VerifyCodeHelper.SaveVerifyCode(code);

            return File(bytes, @"image/jpeg");
        }
    }
}
